package cn.gz.demo.ui.recylerview_header;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import cn.gz.demo.R;
import cn.gz.demo.databinding.RecylerViewBinding;
import cn.gz.demo.ui.base.BaseActivity;

/**
 * Created by Liuyz on 2018/8/10.
 */

public class RecylerViewListActivity extends BaseActivity {
    private RecylerViewBinding binding;
    private RecyclerAdapter adapter;
    private List<String> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);

        initView();
        initData();
    }

    private void initView() {
        binding.recyclerview.setLayoutManager(new LinearLayoutManager(this));
        adapter = new RecyclerAdapter();
        binding.recyclerview.setAdapter(adapter);
    }

    private void initData() {
        list = new ArrayList<>();
        for (int i = 0; i < 25; i++) {
            list.add("item " + i);
        }
        adapter.notifyDataSetChanged();
    }

    private class RecyclerAdapter extends RecyclerView.Adapter {

        private final static int HEAD_COUNT = 1; // 1.代表有头部 0.没有
        private final static int FOOT_COUNT = 0; // 1.代表有尾部 0.没有

        private final static int TYPE_HEAD = 0;
        private final static int TYPE_CONTENT = 1;
        private final static int TYPE_FOOTER = 2;

        public int getContentSize() {
            return list.size();
        }

        @Override
        public int getItemViewType(int position) {
            int contentSize = getContentSize();
            if (HEAD_COUNT != 0 && position == 0) { // 头部
                return TYPE_HEAD;
            } else if (FOOT_COUNT != 0 && position == HEAD_COUNT + contentSize) { // 尾部
                return TYPE_FOOTER;
            } else {
                return TYPE_CONTENT;
            }
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if (viewType == TYPE_HEAD) {
                View itemView = LayoutInflater.from(RecylerViewListActivity.this).inflate(R.layout.head_for_recyclerview, parent, false);
                return new HeadHolder(itemView);
            } else if (viewType == TYPE_CONTENT) {
                View itemView = LayoutInflater.from(RecylerViewListActivity.this).inflate(R.layout.item_for_recycler_view, parent, false);
                return new ContentHolder(itemView);
            } else {
                View itemView = LayoutInflater.from(RecylerViewListActivity.this).inflate(R.layout.foot_for_recyclerview, parent, false);
                return new FootHolder(itemView);
            }
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            if (holder instanceof HeadHolder) { // 头部

            } else if (holder instanceof ContentHolder) { // 内容
                ContentHolder myHolder = (ContentHolder) holder;
                if (HEAD_COUNT != 0)
                    myHolder.itemText.setText(list.get(position - 1));
                else
                    myHolder.itemText.setText(list.get(position));
                myHolder.itemText.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (HEAD_COUNT != 0)
                            Toast.makeText(context, "" + list.get(position - 1), Toast.LENGTH_SHORT).show();
                        else
                            Toast.makeText(context, "" + list.get(position), Toast.LENGTH_SHORT).show();
                    }
                });
            } else { // 尾部

            }
        }

        @Override
        public int getItemCount() {
            return list.size() + HEAD_COUNT + FOOT_COUNT;
        }

        // 头部
        private class HeadHolder extends RecyclerView.ViewHolder {
            public HeadHolder(View itemView) {
                super(itemView);
            }
        }

        // 内容
        private class ContentHolder extends RecyclerView.ViewHolder {
            TextView itemText;

            public ContentHolder(View itemView) {
                super(itemView);
                itemText = (TextView) itemView.findViewById(R.id.item_text);
            }
        }

        // 尾部
        private class FootHolder extends RecyclerView.ViewHolder {
            public FootHolder(View itemView) {
                super(itemView);
            }
        }

    }
}
